import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
public class testfile {
    private static StringBuilder StringBuilder;
    
    private static String ReadFile(String pathFile)
 {
 try
 {

 FileInputStream fileInPutStream = new FileInputStream(pathFile);
     StringBuilder stringBuilder;
     try (Reader reader = new java.io.InputStreamReader(fileInPutStream, "Unicode")) {
         BufferedReader buffReader = new BufferedReader(reader);
         stringBuilder = new StringBuilder();
         String line=null;
         while ((line = buffReader.readLine()) != null)
         {
             StringBuilder /*append*/ = stringBuilder.append(line).append("\n");
         }
     }
 return stringBuilder.toString();
 }
 catch(IOException io)
 {
 System.out.println("Khong tim thay file " + pathFile);
 }
 return "";
 }
 //ghi file
 private static boolean WriteFile(String pathFile, String text, boolean overwrite)
 {
 try
 {
 String oldText = "";
 if(!overwrite)
 {
 oldText = ReadFile(pathFile);
 }
 FileOutputStream fileOutStream = new FileOutputStream(pathFile);
     try (Writer writer = new OutputStreamWriter(fileOutStream,"Unicode")) {
         writer.write(oldText + text);
     }
 return true;
 }
 catch(IOException io)
 {
 System.out.println("Khong ghi duoc file" + pathFile);
 }
 return false;
 }
 // ham main
 public static void main(String[] args)
 {
 String pathFile = "D:\\luan.txt";
 System.out.print(ReadFile(pathFile));
 WriteFile(pathFile, "Nguyen Van Luan", true); 
 System.out.print(ReadFile(pathFile));
 }
}
